﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Mimoza.Plugins.DisplaySettings
{
    public class L66_CE6_ExecutePlugin : BaseExecutePlugin
    {
        const string cRegexTemplate = "(\\w+):\\s+{0}\\s+{1}Hz";
        const string cRegexTemplateLcd = "(\\w+):\\s+{0},\\s+single";

        #region BaseExecutePlugin Members

        public override string Id
        {
            get { return this.ToString(); }
        }

        public override bool Add(Mimoza.ObjectLayer.Policy policy, DisplayResolution data)
        {
            return Apply(policy, data);
        }

        public override bool Remove(Mimoza.ObjectLayer.Policy policy, DisplayResolution data)
        {
            return false;
        }

        public override bool Update(Mimoza.ObjectLayer.Policy policy, DisplayResolution data)
        {
            return Apply(policy, data);
        }

        #endregion

        bool Apply(Mimoza.ObjectLayer.Policy policy, DisplayResolution data)
        {
            string settingString = GetSettingsString(data);
            string regexStr = string.Format(cRegexTemplate, settingString, data.Freq.ToString());
            string regexStrLCD = string.Format(cRegexTemplateLcd, GetResolutionString(data));
            object displayType = 2; // LCD

            Common.Logger.Log.Trace("Mimoza.Plugins.DisplaySettings.L66_CE6_ExecutePlugin.Apply regexStr - '{0}'",
                   regexStr);

            object modeNum = GetModeNumber(regexStr);
            //object lcdIndex = GetLCDIndex(regexStrLCD);

            Common.Logger.Log.Trace("Mimoza.Plugins.DisplaySettings.L66_CE6_ExecutePlugin.Apply modeNum - {0}",
                    modeNum.ToString());

            Common.Instrumental.Registry registry = new Mimoza.Common.Instrumental.Registry("Drivers\\Display\\VIA");

            registry.CheckAndRewrite("DisplayModeNum", modeNum);
            //registry.CheckAndRewrite("DisplayDevice", displayType);
            //registry.CheckAndRewrite("LCDPanelSizeIndex", lcdIndex);

            return registry.IsModified;
        }

        private static object GetModeNumber(string regexStr)
        {
            Regex regex = new Regex(regexStr, RegexOptions.Multiline);
            Match match = regex.Match(Resource.L66Data);
            if (match != null && match.Success)
            {
                return Convert.ToInt32(match.Groups[1].Value, 16);
            }

            Common.Logger.Log.Error("Mimoza.Plugins.DisplaySettings.L66_CE6_ExecutePlugin.GetModeNumber settings not match.");
            throw new System.ArgumentException();
        }

        private static object GetLCDIndex(string regexStr)
        {
            Regex regex = new Regex(regexStr, RegexOptions.Multiline);
            Match match = regex.Match(Resource.L66Data);
            if (match != null && match.Success)
            {
                return Convert.ToInt32(match.Groups[1].Value, 16);
            }

            Common.Logger.Log.Error("Mimoza.Plugins.DisplaySettings.L66_CE6_ExecutePlugin.GetLCDIndex settings not match.");

            return null;
        }

        private static string GetSettingsString(DisplayResolution data)
        {
            return data.Width.ToString() + "x" + data.Height.ToString() + "x" + data.ColorDepthAsInt.ToString();
        }

        private static string GetResolutionString(DisplayResolution data)
        {
            return data.Width.ToString() + "x" + data.Height.ToString();
        }
    }
}
